package team27.service;

import java.util.ArrayList;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import team27.messaging.Chatroom;
import team27.messaging.Message;
import team27.messaging.MessageDao;

//maps this resource to the URL orders
@Path("/")
public class MessageService 
{
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	
	// Return the list of orders for applications with json or xml formats
	@GET
	@Path( "GET/messages" )
	@Produces(MediaType.APPLICATION_JSON)
	public ArrayList<Message> getMessages() 
	{
		System.out.println( Chatroom.messages );
		if( Chatroom.messages.size() > 0 )
			return Chatroom.messages;
		else return null;
	}
	
	@GET
	@Path("GET/login")
	@Produces(MediaType.APPLICATION_JSON)
	public String createUser() 
	{
		int newUser = ++Chatroom.NextUser;
		Chatroom.users.put( newUser, 0 );
		return Integer.toString( newUser );
	}
	
	@GET
	@Path("GET/logout")
	@Produces(MediaType.APPLICATION_JSON)
	public String removeUser( @QueryParam("id") String id ) 
	{
		if( id == null )
		{
			return "Invalid user";
		}
		String charges = getCurrentCharges( id );
		Chatroom.users.remove( Integer.parseInt( id ) );
		return charges;
	}
	
	@GET
	@Path("GET/charges")
	@Produces(MediaType.APPLICATION_JSON)
	public String getCharges( @QueryParam("id") String id ) 
	{
		return getCurrentCharges( id  );
	}
	
	@PUT
	@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public Response putMessage( JAXBElement<Message> msg )
	{
		int user = Integer.parseInt( msg.getValue().id );
		if( !Chatroom.users.containsKey(user))
		{
			return Response.status(Status.FORBIDDEN ).build();
		}
		
		Chatroom.messages.add( msg.getValue() );
		int count = Chatroom.users.get(user);
		count += msg.getValue().message.length();
		Chatroom.users.put( user, count );
		return putAndGetResponse(msg.getValue() , Integer.toString( count ) ) ;
	}
	
	
	
	
	private String getCurrentCharges( String id )
	{
		if( id == null )
			return "Invalid user";
		
		int user = Integer.parseInt(id);
		
		if( !Chatroom.users.containsKey( user ) )
		{
			return "Invalid user";
		}
		
		int count = Chatroom.users.get( user );
		
		double asMoney = count * 0.10;
		
		return String.format( "%.2f", asMoney );
	}
	
	 private Response putAndGetResponse(Message msg , String count) {
		    Response res;
		    int nc = 0;
		    if (count != null )
		    	nc = Integer.parseInt(count);
		    
		    String newCount = Integer.toString( nc  + msg.message.length());
		    res = Response.ok().cookie(new NewCookie ("count", newCount )).build();		    
		    MessageDao.instance.getModel().put(msg.id, msg);
		    return res;
		    
	 }
	
	
	
}


